<?php

namespace api\modules\v1\controllers\clinic;

use Yii;
use yii\web\HttpException;
use common\models\clinic\Clinic;
use api\controllers\OnAuthController;
use services\common\CommonService;

class TreatmentPatientController extends OnAuthController
{
    public $modelClass = [];

    /**
     * @api {get} /v1/clinic/treatment-patient/index 所有用户治疗列表
     * @apiName index
     * @apiGroup treatment-patient
     * @apiVersion 1.0.2
     *
     * @apiParam {Number} clinic_project_id                                治疗项目id
     * @apiParam {Number} clinic_id                                        诊室id
     * @apiParam {Number} page                                             页码
     * @apiParam {Number} pageSize                                         每页显示数
     * @apiParam {String} start_time                                       开始时间
     * @apiParam {String} end_time                                         结束时间
     *
     */
    public function actionIndexList()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $params['project_id'] = $this->projectId;
        $list = Yii::$app->services->clinicTreatmentPatient->getList($params, $page, $pageSize);

        //诊室筛选框
        $condition['select'] = 'id as value,name as title';
        $condition['status'] = Clinic::STATUS_ENABLE;
        $condition['project_id'] = $this->projectId;
        $clinic = Yii::$app->services->clinic->findList($condition);

        //治疗项目筛选框
        $clinicProject = Yii::$app->services->clinicProject->findList($condition);
        return [
            'list' => $list,
            'clinic' => $clinic,
            'clinic_project' => $clinicProject,
            'outpatient_type' => CommonService::getOutpatientTypeList(0),
        ];
    }


    /**
     * @api {get} /v1/clinic/treatment-patient/treatment-list 患者治疗列表
     * @apiName treatment-list
     * @apiGroup treatment-patient
     * @apiVersion 1.0.2
     *
     * @apiParam {String} patient_card_id                                  患者诊疗号
     * @apiParam {Number} clinic_project_id                                治疗项目id
     * @apiParam {Number} page                                             页码
     * @apiParam {Number} pageSize                                         每页显示数
     * @apiParam {String} start_time                                       开始时间
     * @apiParam {String} end_time                                         结束时间
     *
     */
    public function actionTreatmentList()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $params['project_id'] = $this->projectId;
        if (!isset($params['patient_card_id']) || !$params['patient_card_id']) {
            throw new HttpException(422, '参数错误');
        }

        // 新增、治疗、退费列表
        $list = Yii::$app->services->clinicTreatmentPatient->getTreatmentList($params, $page, $pageSize);

        // 患者信息
        $userPatient = Yii::$app->services->clinicTreatmentPatient->getPatientDetail($params);

        // 患者治疗剩余
        $userSurplus = Yii::$app->services->clinicTreatmentPatient->getPatientSurplus($params);

        $patientTreatmentProjectList = array_column($userSurplus, 'clinic_project_name', 'clinic_project_id');
        $patientTreatmentProject = [];
        foreach ($patientTreatmentProjectList as $k => $v) {
            $patientTreatmentProject[] = [
                'clinic_project_id' => $k,
                'clinic_project_name' => $v,
            ];
        }

        return [
            'list' => $list,
            'user_patient' => $userPatient,
            'user_surplus' => $userSurplus,
            'treatment_project' => $patientTreatmentProject
        ];
    }

    /**
     * @api {get} /v1/clinic/treatment-patient/treatment-detail 患者治疗详细
     * @apiGroup treatment-list
     * @apiName treatment-detail
     * @apiVersion 1.0.2
     *
     * @apiParam {Number} clinic_treatment_id                                治疗项目id
     */
    public function actionTreatmentDetail()
    {
        $params = Yii::$app->request->get();
        if (!isset($params['clinic_treatment_id']) || !$params['clinic_treatment_id']) {
            throw new HttpException(422, '参数错误');
        }
        $treatmentDetail = Yii::$app->services->clinicTreatmentPatient->treatmentDetail($params['clinic_treatment_id']);
        $treatment = Yii::$app->services->clinicTreatmentPatient->treatment($params['clinic_treatment_id']);
        return [
            'user_patient' => $treatment,
            'list' => $treatmentDetail,
        ];
    }

    /**
     * @api {get} /v1/clinic/treatment-patient/get-treatment-num
     * @apiGroup treatment-patient
     * @apiName get-treatment-num
     * @apiVersion 1.0.2
     *
     * @apiDescription 患者治疗项目剩余
     *
     * @apiParam {String} patient_card_id  诊疗号
     *
     */
    public function actionGetTreatmentNum()
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = $this->projectId;
        if (!isset($params['patient_card_id']) || !$params['patient_card_id']) {
            throw new HttpException(422, '参数错误');
        }
        $treatmentNum = Yii::$app->services->clinicTreatmentPatient->getTreatmentNum($params);
        return $treatmentNum;
    }

    /**
     * @api {post} /v1/clinic/treatment-patient/add-treat-num 新增治疗数量
     * @apiGroup treatment-patient
     * @apiName add-treat-num
     * @apiVersion 1.0.2
     *
     * @apiDescription 新增治疗
     *
     * @apiParam {String} patient_card_id       诊疗号
     * @apiParam {Number} clinic_project_id     治疗项目ID
     * @apiParam {Number} total_num             正常数量
     * @apiParam {Number} help_num              援助数量
     *
     */
    public function actionAddTreatNum()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;
        $params['member_id'] = Yii::$app->user->identity->member_id;
        $params['member_name'] = Yii::$app->user->identity->member->realname;
        if (!isset($params['patient_card_id']) || !$params['patient_card_id']) {
            throw new HttpException(422, '参数错误');
        }
        if (!isset($params['total_num']) || $params['help_num'] < 0) {
            throw new HttpException(422, '正常数量不能小于0');
        }
        if (!isset($params['help_num']) || $params['help_num'] < 0) {
            throw new HttpException(422, '援助数量不能小于0');
        }

        $res = Yii::$app->services->clinicTreatmentPatient->addTreatNum($params);
        return $res;
    }

    /**
     * @api {post} /v1/clinic/treatment-patient/refund-treat-num 退费治疗数量
     * @apiGroup treatment-patient
     * @apiName refund-treat-num
     * @apiVersion 1.0.2
     *
     * @apiDescription 退费（退治疗）
     *
     * @apiParam {String} $param1  参数说明
     */
    public function actionRefundTreatNum()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;
        $params['member_id'] = Yii::$app->user->identity->member_id;
        $params['member_name'] = Yii::$app->user->identity->member->realname;
        if (!isset($params['patient_card_id']) || !$params['patient_card_id']) {
            throw new HttpException(422, '参数错误');
        }
        if (!isset($params['total_num']) || $params['help_num'] < 0) {
            throw new HttpException(422, '正常数量不能小于0');
        }
        if (!isset($params['help_num']) || $params['help_num'] < 0) {
            throw new HttpException(422, '援助数量不能小于0');
        }
        $res = Yii::$app->services->clinicTreatmentPatient->refundTreatNum($params);
        return $res;

    }

}
